<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-row class="search-row">
        <!-- <el-col :span="5">
         <el-form-item label="告警ID" prop="warnid">
           <el-input
             style="width: 150px;"

             v-model="queryParams.warnid"
             placeholder="请输入告警信息id"
             clearable
             @keyup.enter.native="handleQuery"
           />
         </el-form-item>
        </el-col> -->
        <el-col>
          <el-form-item label="抗定位方式" prop="method">
            <el-input
              v-model="queryParams.method"
              placeholder="请输入抗定位方式"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="混淆位置" prop="targetLocation">
            <el-input
              v-model="queryParams.targetLocation"
              placeholder="请输入目标混淆位置"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col>
          <el-form-item label="任务状态" prop="state">
            <el-select
              v-model="queryParams.state"
              placeholder="请选择任务状态"
              clearable
            >
              <el-option
                v-for="item in dict.type.antilocalization_task_states"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <!--        <el-col :span="4">-->
        <!--          <el-form-item label="抗定位后纬度" prop="resultLon">-->
        <!--            <el-input-->
        <!--              style="width: 150px;"-->

        <!--              v-model="queryParams.resultLon"-->
        <!--              placeholder="请输入抗定位后纬度"-->
        <!--              clearable-->
        <!--              @keyup.enter.native="handleQuery"-->
        <!--            />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col>
          <el-form-item label="开始时间" prop="startime">
            <el-date-picker
              clearable
              v-model="queryParams.startime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择任务执行开始时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="结束时间" prop="endtime">
            <el-date-picker
              clearable
              v-model="queryParams.endtime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择任务执行结束时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row> </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8" style="margin: 0">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:task:add']"
          >新增
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:task:edit']"
        >修改
        </el-button>
      </el-col> -->

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:task:export']"
          >导出
        </el-button>

        <el-button
          v-hasPermi="['system:task:remove']"
          plain
          size="mini"
          class="inBtn"
          @click="handleDelete"
        >
          <svg-icon class="search-icon" icon-class="shanchu" />
          批量删除
        </el-button>
        <el-button plain size="mini" class="inBtn" @click="handleBatchStop">
          <svg-icon class="search-icon" icon-class="shanchu" />
          停止任务
        </el-button>
      </el-col>
      <el-col :span="5">
        <el-button
          type="primary"
          class="inBtn"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-col>
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table
      max-height="610"
      v-loading="loading"
      :data="taskList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="id"/>-->
      <!--      <el-table-column label="告警信息id" align="center" prop="warnid"/>-->
      <el-table-column label="用户ID" align="center" prop="userId" />

      <el-table-column label="抗定位方式" align="center" prop="method" />
      <el-table-column
        label="目标混淆位置"
        align="center"
        prop="targetLocation"
      />
      <!--      <el-table-column label="任务状态" align="center" prop="state"/>-->
      <el-table-column prop="resultLon" label="任务状态" show-overflow-tooltip>
        <template slot-scope="scope">
          <div style="display: flex">
            <div
              style="
                display: flex;
                flex-direction: column;
                align-items: center;
                margin-right: 10px;
              "
            >
              {{ scope.row.state }}
            </div>
            <jwd-progress
              style="flex: auto"
              v-if="scope.row.state == '已完成'"
              :percentage="100"
            />
            <jwd-progress
              style="flex: auto"
              v-else-if="scope.row.state == '正在进行'"
              :percentage="50"
            />
            <jwd-progress style="flex: auto" v-else :percentage="0" />
          </div>
        </template>
      </el-table-column>
   
      
      <!-- 是否定位成功 -->
      <el-table-column label="是否定位成功" align="center" prop="isSuccess" >
        <template slot-scope="scope">
          <span>{{ scope.row.state == '已完成' ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="抗定位后地点" align="center" prop="resultLocation"/> -->
      <el-table-column label="抗定位后经度" align="center" prop="resultLat" />
      <el-table-column label="抗定位后纬度" align="center" prop="resultLon" />

      <el-table-column
        label="任务执行开始时间"
        align="center"
        prop="startime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="任务执行结束时间"
        align="center"
        prop="endtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endtime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="任务执行日志" align="center" prop="methodLog"/>-->
      <el-table-column
        width="300"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <div
            style="
              text-align: right;
              display: flex;
              flex-direction: row;
              align-items: center;
            "
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:task:edit']"
              v-show="scope.row.state == '已停止'"
              >修改
            </el-button>
            <div
              style="margin-right: 10px"
              v-if="scope.row.method == '社交内容混淆'"
            >
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-share"
                @click="handleShare(scope.row)"
                v-hasPermi="['system:task:remove']"
                >微博分享
              </el-button> -->
            </div>
            <div
              style="margin-right: 10px"
              v-if="scope.row.method == '社交关系混淆'"
            >
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-share"
                @click="handleSocialRelationModify(scope.row)"
                v-hasPermi="['system:task:remove']"
                >社交关系修改
              </el-button> -->
            </div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleDetails(scope.row)"
              v-hasPermi="['system:task:remove']"
              >详情
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:task:remove']"
              >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleConfuse(scope.row)"
              v-hasPermi="['system:task:remove']"
              v-if="scope.row.method=='社交内容混淆'"
              >查看混淆博文
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改抗定位任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <div v-if="riskInfo && title == '修改抗定位任务'">
        <el-descriptions title="风险信息" :column="2">
          <el-descriptions-item label="用户名">
            {{ riskInfo.username }}
          </el-descriptions-item>
          <el-descriptions-item label="泄露地点">
            {{ riskInfo.locationLeak }}
          </el-descriptions-item>
          <el-descriptions-item label="泄露坐标">
            ({{ riskInfo.locationLat }},{{ riskInfo.locationLon }})
          </el-descriptions-item>
          <el-descriptions-item label="定位地点">
            {{ riskInfo.locationPos }}
          </el-descriptions-item>
        </el-descriptions>
        <hr />
      </div>
      <el-form
        inline
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="告警ID" prop="warnid">
          <el-input v-model="form.warnid" placeholder="请输入告警信息id" />
        </el-form-item>
        <el-form-item label="抗定位方式" prop="method">
          <el-input v-model="form.method" placeholder="请输入抗定位方式" />
        </el-form-item>
        <el-form-item label="目标混淆位置" prop="targetLocation">
          <el-input
            v-model="form.targetLocation"
            placeholder="请输入目标混淆位置"
          />
        </el-form-item>
        <el-form-item label="任务状态" prop="state">
          <el-select
            v-model="form.state"
            placeholder="请选择任务状态"
            clearable
          >
            <el-option
              v-for="item in dict.type.antilocalization_task_states"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="抗定位后地点" prop="resultLocation">
          <el-input
            v-model="form.resultLocation"
            placeholder="请输入抗定位后地点"
          />
        </el-form-item>
        <el-form-item label="抗定位后经度" prop="resultLat">
          <el-input v-model="form.resultLat" placeholder="请输入抗定位后经度" />
        </el-form-item>
        <el-form-item label="抗定位后纬度" prop="resultLon">
          <el-input v-model="form.resultLon" placeholder="请输入抗定位后纬度" />
        </el-form-item>
        <el-form-item label="任务执行开始时间" prop="startime">
          <el-date-picker
            clearable
            v-model="form.startime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务执行开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务执行结束时间" prop="endtime">
          <el-date-picker
            clearable
            v-model="form.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择任务执行结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务执行日志" prop="methodLog">
          <el-input v-model="form.methodLog" placeholder="请输入任务执行日志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="inBtn" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 提示修改社交关系 -->
    <el-dialog
      title="修改社交关系"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <span>您需要修改的社交关系为：</span>
      <span>{{ dialogVisibleText }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="nextFn"
          >下一步</el-button
        >
      </span>
    </el-dialog>

    <!-- 查看混淆博文 -->
    <el-dialog
      title="混淆博文"
      :visible.sync="confuseFlag"
      width="30%">
      <el-descriptions  :column="1" border :labelStyle="{width:'80px'}" v-if="confuseContent">
        <el-descriptions-item label="混淆文本">{{ confuseContent.confuseText }}</el-descriptions-item>
        <el-descriptions-item label="混淆图片">
          <span v-for="item in confuseContentImg">
            <el-image
          style="width: 100px; height: 100px"
          :src="fileUrl+item"
          ></el-image>
          </span>
          <!-- <el-image
          style="width: 100px; height: 100px"
          :src="fileUrl+confuseContent.confuseImgs"
          ></el-image> -->
        </el-descriptions-item>
      </el-descriptions>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="confuseFlag = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask,
} from "@/api/combatUser/antiLocalization";
import jwdProgress from "@/components/jwdProgress/index.vue";
import { detailsInfo } from "@/api/combatUser/alarmInformation";
import {getMinioUrl} from "@/api/combat/userWb";

export default {
  name: "Task",
  components: {
    jwdProgress,
  },
  dicts: ["antilocalization_task_states"],
  data() {
    return {
      // 修改社交关系
      dialogVisible:false,
      // 修改的内容
      dialogVisibleText:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抗定位任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warnid: null,
        method: null,
        targetLocation: null,
        state: null,
        resultLocation: null,
        resultLat: null,
        resultLon: null,
        startime: null,
        endtime: null,
        methodLog: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 风险信息
      riskInfo: null,
      // 混淆博文
      confuseContent:null,
      //混淆博文图片
      confuseContentImg:[],

      confuseFlag:false,
      // 文件url
      fileUrl:'',
    };
  },
  created() {
    this.getList();
    getMinioUrl().then(res=>{
      this.fileUrl=res.data
    })
  },
  methods: {
    /** 查询抗定位任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then((response) => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warnid: null,
        method: null,
        targetLocation: null,
        state: null,
        resultLocation: null,
        resultLat: null,
        resultLon: null,
        startime: null,
        endtime: null,
        methodLog: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加抗定位任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      // getTask(id).then(response => {
      detailsInfo(row.warnid).then((res) => {
        if (res.code == 200) {
          this.riskInfo = res.data;
          this.form = row;
          this.open = true;
          this.title = "修改抗定位任务";
        } else {
          this.$modal.msgDanger("数据有误");
        }
      });
      // });
    },
    /*微博分享按钮点击*/
    handleShare(row) {
      // 1. 从row中获取分享内容（根据实际数据结构调整）
      const content = row.confuseText || "";
      const url = row.url || window.location.href;

      // 2. 构造微博分享URL
      const weiboURL = `https://service.weibo.com/share/share.php?${new URLSearchParams(
        {
          title: encodeURIComponent(content),
          type: "3", // 分享类型（3代表默认样式）
          // appkey: '',        // 可选，如果需要关联官方应用
          // ralateUid: '',    // 可选，关联用户UID
          language: "zh_cn", // 语言设置
        }
      ).toString()}`;

      // 3. 打开新窗口（推荐方式）
      const width = 600;
      const height = 400;
      const left = (screen.width - width) >> 1;
      const top = (screen.height - height) >> 1;

      window.open(
        weiboURL,
        "weibo-share",
        `width=${width},height=${height},top=${top},left=${left},toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,status=no`
      );
    },

    /*社交关系修改按钮点击*/
    handleSocialRelationModify(row) {
      //弹窗提醒需要需改的社交关系
      this.dialogVisible =true;
      let data = JSON.parse(row.relationContent);
      let text = '';
      data.forEach(item=>{
        text += "关注："+item.name;
      })
      this.dialogVisibleText = text;
    },

      //打开微博个人中心页面
    nextFn() {
      // this.dialogVisible = false;
      //https://weibo.com/u/page/follow/6373742334
      window.open(
        `https://weibo.com/u/page/follow/6373742334`,
        "_blank"
      );
    },
    /*详情按钮点击*/
    handleDetails(row) {
      this.$router.push({
        path:
          "/userPositioningScreen/compare?id=" +
          row.id +
          "&warnid=" +
          row.warnid,
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.state = "正在进行";
            updateTask(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除抗定位任务编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTask(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 停止按钮操作*/
    handleBatchStop() {
      this.$modal
        .confirm('是否确认停止抗定位任务编号为"' + this.ids + '"的数据项？')
        .then(() => {
          this.$modal.msgSuccess("停止成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/task/export",
        {
          ...this.queryParams,
        },
        `task_${new Date().getTime()}.xlsx`
      );
    },
    // 查看混淆博文按钮
    handleConfuse(row){
      this.confuseContent=row
      let Imgs=[]
      
      if(row.confuseImgs ){
        //判断row.confuseImgs中是否有逗号分割，有的话分割成数组赋值，没有的话直接复制
        if(row.confuseImgs.indexOf(',') !== -1){
          // 如果有逗号，分割成数组
          Imgs = row.confuseImgs.split(',');
        } else {
          // 如果没有逗号，转换为数组
          Imgs = [row.confuseImgs];
        }
      }
      this.confuseContentImg=Imgs
      this.confuseFlag=true
    },
  },
};
</script>
<style scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.inBtn {
  color: white;
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%);
  border-radius: 4px 4px 4px 4px;
}

::v-deep .el-pager li.active {
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%) !important;
}
::v-deep .cell {
  text-align: center;
}
.search-row {
  display: flex;
  .el-form-item {
    display: flex;
  }
}
</style>
